import * as THREE from 'three'
import { gui, debugObject } from '../system/gui'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js'
import { ReflectorForSSRPass } from "three/examples/jsm/objects/ReflectorForSSRPass";
import { ssrPass } from '../base/composer';
import { SSRPass } from 'three/examples/jsm/postprocessing/SSRPass';
import isMobileDevice from '../utils/deviceType';
import { createMultiMaterialObject } from 'three/examples/jsm/utils/SceneUtils.js';

let scene = null
let url = import.meta.env.BASE_URL + 'public/models/draco/gltf/' + '1b-s-data3-1-0Draco.glb'
let model = null
let x=0,y=0,z=0,sx=1,sy=1,sz=1;
let wellItems = new THREE.Object3D();
let wellSprites = new THREE.Object3D();

function removeLoading(){
    document.querySelector(".loader-container").classList.add("loaded")
    document.querySelector(".loader").setAttribute("style", "  border-right-color: #0000;")
}

const loadingManager = new THREE.LoadingManager(
    // Loaded
    () => {
        console.log('Loaded successfully!')
        setTimeout(() => {
            removeLoading()
        }, 1500);
        model.name="oil-mesh"
        scene.add(model)
    },
    // Progress
    (itemUrl, itemsLoaded, itemsTotal) => {
        let progressRatio = itemsLoaded / itemsTotal
        let p = (progressRatio * 100).toFixed(0)
        console.log('progress:',p,'%')
    }
)
const gltfLoader = new GLTFLoader(loadingManager)
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath(import.meta.env.BASE_URL + 'draco/')
gltfLoader.setDRACOLoader(dracoLoader)

const matParams = {
    vertexColors:true,//使用顶点颜色渲染
    //color: color,
    color: new THREE.Color(parseInt("0xb69860")),
    // 渲染两面
    side: THREE.DoubleSide,
    //wireframe: true, //材质的网格wireframe属性
    clearcoat: 1.0,//物体表面清漆层或者说透明涂层的厚度
    clearcoatRoughness: 0.1,//透明涂层表面的粗糙度
    transmission: 0.5,//物理材质透光率,模拟玻璃、半透明塑料一类的视觉效果,代替普通透明属性.opacity 设置Mesh透明度,即便完全透射的情况下仍可保持高反射率。
    ior: 1.5,//折射率.ior非金属材料的折射率从1.0到2.333。默认值为1.5。
    metalness: 0.9,//金属度
    roughness: 0.5,//粗糙度
    bumpScale: 0.5, // 凹凸贴图的高度
    envMapIntensity: 2.5, //环境贴图对Mesh表面影响程度
    opacity: 0.9,
    // 我们同样需要把tansparent属性打开
    transparent: true,
}
const glassMaterial = new THREE.MeshPhysicalMaterial(matParams);

/**
 * 
 * @param {THREE.Scene} _scene 
 */
function createModels(_scene) {
    scene = _scene

    // 背景音
    const positionalAudio = new THREE.PositionalAudio(scene.userData.listener);
    const audioLoader = new THREE.AudioLoader();
    audioLoader.load(import.meta.env.BASE_URL + 'music/bg0.mp3', function (buffer) {
        positionalAudio.setBuffer(buffer);
        positionalAudio.setRefDistance(3000);

        // 浏览器安全策略，须用户进行交互才可播放音频
        addEventListener("click", () => {
            !positionalAudio.isPlaying && positionalAudio.play();
        })
        addEventListener("touchend", () => {
            !positionalAudio.isPlaying && positionalAudio.play();
        })
    });

    // 加载器解析模型
    gltfLoader.load(url, (gltf) => {
        model = gltf.scene;
        
        model.add(positionalAudio)
        model.castShadow = true;
        model.receiveShadow = true;

        //console.log('gltf',gltf);
        //gltf.scene获取gltf文件包含的模型数据
        //gltf模型添加到三维场景中

        //WebGL 使用 OpenGL 惯例，其中 Y 轴朝上，Z 轴朝向观看者。WebGPU 使用 Metal 惯例，其中 Y 轴在屏幕之外，Z 轴在屏幕范围外。
        //请注意，在帧缓冲区坐标、视口坐标和片段/像素坐标中，Y 轴方向为向下。在剪辑空间中，Y 轴方向仍然与 WebGL 中的方向相同。
        x = 3850/1, y = -1800/1, z= 1750/1,  
        //sx = 0.001, sy = 0.001, sz = 0.001;
        // 几何体沿着x轴平移
        model.translateX(x);
        // 几何体沿着y轴平移
        model.translateY(y);
        // 几何体沿着Z轴平移
        model.translateZ(y);

        // 修改模型的缩放
        model.scale.set(sx, sy, sz); // 设置缩放比例 (x, y, z)
        // 几何体绕着x轴旋转90度
        //model.rotateZ(Math.PI);
        // 几何体绕着x轴旋转90度
        model.rotateY(-Math.PI / 12);
        // 几何体绕着x轴旋转90度
        model.rotateX(-Math.PI);
        model.rotateZ(-Math.PI);

        model.position.z += 1750*2.5

        let meshCount = 0;
        let colorCube = parseInt(colorList[0]);
        let colorCount = 0;
        let color = new THREE.Color(colorCube);

        model.traverse((child) =>{
            if (child.isMark || child.isMarkChild || child.isLine || child.isSprite) return;
            if (child.isMesh) {
                // child.geometry.center();
                // child is an instance of Mesh
                child.receiveShadow = true; 
                child.frustumCulled = false;
                //模型阴影
                child.castShadow = true;
                //模型自发光
                child.material.emissive = child.material.color;
                child.material.emissiveMap = child.material.map;
                if(!(meshCount%1)){
                    colorCount++
                    if(colorCount>colorList.length){
                        colorCount = 0;
                        }
                    colorCube = parseInt(colorList[colorCount]);
                    color = new THREE.Color(colorCube);
                    
                    //console.log(meshCount)
  
                    const colorBuffer = addColorBuffer(child.geometry);
                    child.geometry.attributes.color = new THREE.BufferAttribute(colorBuffer, 3); 

                }

                // 替换材质
                child.material = glassMaterial;

                meshCount++
            }
        })

        

        //console.log('model', model)
        updateAllMaterials()

        gui.close()

        //initExplodeModel(model)
        //onChnageModelView(10);
    })

    const ground = new THREE.Mesh(
        new THREE.PlaneGeometry(5000, 6000),
        new THREE.MeshStandardMaterial({
            color: '#7780a6',
            roughness: 0.2,
            metalness: 0.8,
            side: THREE.FrontSide,
            envMap: scene.userData.dynamicMap,
            envMapIntensity: 0.8,
        })
    );

    ground.layers.enable(scene.userData.rtCubeCameraLayer)
    ground.receiveShadow = true
    ground.rotation.set(-Math.PI * 0.5, 0, 0)
    ground.position.set(0, 0, 0)
    ground.name = 'groundMesh'
    scene.add(ground)

    let isUseDepthTexture = true
    if (isMobileDevice()) {
        isUseDepthTexture = false
    }
    const groundReflector = new ReflectorForSSRPass(new THREE.PlaneGeometry(5, 6), {
        clipBias: 0.0003,
        textureWidth: window.innerWidth,
        textureHeight: window.innerHeight,
        color: 0x888888,
        useDepthTexture: isUseDepthTexture,
    });

    groundReflector.material.depthWrite = true;
    groundReflector.rotation.set(- Math.PI * 0.5, 0, 0);
    groundReflector.position.set(0, 0.01, 0)
    groundReflector.visible = false;
    ssrPass.groundReflector = groundReflector
    scene.add(groundReflector);
    // setting
    ground.renderOrder = 1
    groundReflector.renderOrder = 2
    groundReflector.fresnel = ssrPass.fresnel = true;
    groundReflector.distanceAttenuation = ssrPass.distanceAttenuation = true;
    ssrPass.maxDistance = 1.3;
    groundReflector.maxDistance = ssrPass.maxDistance;
    ssrPass.opacity = 0.55;
    groundReflector.opacity = ssrPass.opacity;

    // debug
    debugObject.paintColor = '#b3b3ff'
    gui.addColor(debugObject, 'paintColor').onChange(() => {
        updateAllMaterials()
    })
    gui.add(ground, 'visible').name('Ground Visible')
    // gui.add(ground.position, 'y').min(-10).max(10).step(0.001).name('Ground Y')
    // gui.addColor(ground.material, 'color').name('Ground Color')
    // gui.add(ground.material, 'roughness').min(0).max(1).step(0.001).name('Ground Roughness')
    // gui.add(ground.material, 'metalness').min(0).max(1).step(0.001).name('Ground Metalness')
    // gui.add(ground.material, 'envMapIntensity').min(0).max(10).step(0.001).name('Ground EnvMapIntensity')

    createBatchMesh();
    let wellTemp = createMesh(0,550,0);
    wellTemp.name = "wellTemp";
    scene.add(wellTemp);
  
    addBox(0,1250,0);
    addSphere(0,1250,0);
  
    //onChnageModelView(10);

}

/*
实现原理
初始化数据：遍历计算每个mesh的中心点，保存在userData上
爆炸计算逻辑：以当前中心的到原点的矢量A作为爆炸方向，|A| * d(爆炸系数 ) 作为爆炸距离；
渲染：在循环渲染中每次更改 d ,这样就可以实现爆炸动画效果了
*/

// 初始化爆炸数据保存到每个mesh的userdata上
function initExplodeModel(modelObject) {
    if (!modelObject) return;
  
    // 计算模型中心
    const explodeBox = new THREE.Box3();
    explodeBox.setFromObject(modelObject);
    const explodeCenter = getWorldCenterPosition(explodeBox);
  
    const meshBox = new THREE.Box3();
  
    // 遍历整个模型，保存数据到userData上，以便爆炸函数使用
    modelObject.traverse(function (value) {
      if (value.isLine || value.isSprite) return;
      if (value.isMesh) {
        meshBox.setFromObject(value);
  
        const meshCenter = getWorldCenterPosition(meshBox);
        // 爆炸方向
        value.userData.worldDir = new THREE.Vector3()
          .subVectors(meshCenter, explodeCenter)
          .normalize();
        // 爆炸距离 mesh中心点到爆炸中心点的距离
        value.userData.worldDistance = new THREE.Vector3().subVectors(meshCenter, explodeCenter);
        // 原始坐标
        value.userData.originPosition = value.getWorldPosition(new THREE.Vector3());
        // mesh中心点
        value.userData.meshCenter = meshCenter.clone();
        value.userData.explodeCenter = explodeCenter.clone();
      }
    });
  }
  
  // 模型爆炸函数 
  const explodeModel = (model, scalar) => {
    model.traverse(function (value) {
      // @ts-ignore
      if (!value.isMesh || !value.userData.originPosition) return;
      const distance = value.userData.worldDir
        .clone()
        .multiplyScalar(value.userData.worldDistance.length() * scalar);
      const offset = new THREE.Vector3().subVectors(
        value.userData.meshCenter,
        value.userData.originPosition
      );
      const center = value.userData.explodeCenter;
      const newPos = new THREE.Vector3().copy(center).add(distance).sub(offset);
      const localPosition = value.parent?.worldToLocal(newPos.clone());
      localPosition && value.position.copy(localPosition);
    });
  };
  //辅助代码
  function getWorldCenterPosition(box, scalar = 0.5) {
    return new THREE.Vector3().addVectors(box.max, box.min).multiplyScalar(scalar);
  }
  
  const onChnageModelView = (val) => {
      explodeModel(model, val) 
  }

let properties = {
    radiusTop: {
      name: 'radiusTop',
      value: 10,
      min: -40,
      max: 40,
      step: 1
    },
    radiusBottom: {
      name: 'radiusBottom',
      value: 20,
      min: -40,
      max: 40,
      step: 1
    },
    height: {
      name: 'height',
      value: 1500,
      min: 0,
      max: 1000,
      step: 1
    },
    segmentsX: {
      name: 'segmentsX',
      value: 8,
      min: 0,
      max: 40,
      step: 1
    },
    segmentsY: {
      name: 'segmentsY',
      value: 8,
      min: 0,
      max: 40,
      step: 1
    },
    openEnded: false
};


let colorList = [
    //卡其色
    '0xb69860',
    //浅黄
    '0xedd3a1',
    //枯色
    '0xe0c38c',
    //榛色
    '0xbfa46f',
    //樺茶色
    '0x726250',
    //朽葉色
    '0x917347',
    //路考茶
    '0x8c7042',
    '0x8c7042',
    //国防色
    '0x7b6c3e',
    '0x7b6c3e',
    //利休茶
    '0xa59564',
    //空五倍子色
    '0x9d896c',
    //生壁色
    '0x94846a',
    //肥後煤竹
    '0x897858',
    //媚茶
    '0x716246',
    //黄唐色
    '0xb98c46',
    //桑染
    '0xb79b5b',
    //黄橡
    '0xb68d4c',
    //芝翫茶
    '0xad7e4e',
    //狐色
    '0xc38743',
    //黄土色
    '0xc39143',
    //香染
    '0xad7d4c',
    //枇杷茶
    '0xae7c4f'
]

const colorsPalette = [
    {color:'0xff0f00', name:'1100'},
    {color:'0xff7d00', name:'1000'},
    {color:'0xffec00', name:'900'},
    {color:'0xa3ff00', name:'800'},
    {color:'0x00ff39', name:'700'},
    {color:'0x00ffa8', name:'600'},
    {color:'0x00e7ff', name:'500'},
    {color:'0x0078ff', name:'300'},
    {color:'0x000aff', name:'100'},
]

// 井筒 WELL 数据 使用大地坐标时，XCOORD、YCOORD的值非常大
// 基准点坐标, 分别为XCOORD0和YCOORD0，XCOORD和YCOORD数组只记录偏移量。
// 基准XCOORD0 + 偏移XCOORD
// 基准YCOORD0 + 偏移YCOORD
let XCOORD0 = 630752.507
let YCOORD0 = 4338916.207 
let infoTIME = 0
/*
WELL 1 'GR1-1' 305 'SHUT'
WELL 2 'GR1-2' 370 'LRAT:999.9999 SM3/DAY'
WELL 3 'GR1-3' 370 'LRAT:999.9999 SM3/DAY'
WELL 4 'GR1-4' 371 'LRAT:999.9928 SM3/DAY'
WELL 5 'GR2-2' 355 'SHUT'
WELL 6 'GR2-3' 356 'LRAT:1000 SM3/DAY'
WELL 7 'GR2-4' 331 'LRAT:1000 SM3/DAY'
WELL 8 'GR2-5' 343 'LRAT:1000 SM3/DAY'
WELL 9 'GR3-2' 389 'LRAT:999.9997 SM3/DAY'
WELL 10 'GR3-3' 363 'SHUT'
WELL 11 'GR3-4' 365 'LRAT:1000 SM3/DAY'
WELL 12 'GR3-6' 363 'LRAT:1000 SM3/DAY'
WELL 13 'GR4-1' 370 'LRAT:999.9999 SM3/DAY'
WELL 14 'GR4-2' 347 'SHUT'
WELL 15 'GR4-3' 319 'LRAT:1000 SM3/DAY'
WELL 16 'GR4-4' 355 'LRAT:1000 SM3/DAY'
WELL 17 'GR4-5' 351 'LRAT:1000 SM3/DAY'
WELL 18 'GR4-6' 332 'LRAT:1000 SM3/DAY'
WELL 19 'GR5-1' 382 'LRAT:1000 SM3/DAY'
WELL 20 'GR5-2' 386 'LRAT:1000 SM3/DAY'
WELL 21 'GR5-5' 318 'SHUT'
WELL 22 'GR5-6' 392 'LRAT:1000 SM3/DAY'
WELL 23 'GR6-2' 357 'LRAT:1000 SM3/DAY'
WELL 24 'GR6-3' 312 'SHUT'
WELL 25 'GR6-4' 316 'LRAT:1000 SM3/DAY'
WELL 26 'GR6-5' 351 'LRAT:1000 SM3/DAY'
WELL 27 'GR7-2' 273 'SHUT'
WELL 28 'GR7-3' 195 'LRAT:999.9997 SM3/DAY'
*/

let wellInfo = [
    {
        id: '1',//井号
        name: 'GR1-1',//井名
        num: '305',//井节点数目
        cpr:'SHUT',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest 
                rest: [
                    {key:'XCOORD',unit:'m',value: '1634.44'},
                    {key:'YCOORD',unit:'m',value: '-513.6248'},
                    {key:'DEPTH',unit:'m',value: '1852.081'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '2',//井号
        name: 'GR1-2',//井名
        num: '370',//井节点数目
        cpr:'LRAT:999.9999 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '1578.101'},
                    {key:'YCOORD',unit:'m',value: '-904.3235'},
                    {key:'DEPTH',unit:'m',value: '1857.264'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '3',//井号
        name: 'GR1-3',//井名
        num: '370',//井节点数目
        cpr:'LRAT:999.9999 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '2040.376'},
                    {key:'YCOORD',unit:'m',value: '-613.6444'},
                    {key:'DEPTH',unit:'m',value: '1884.242'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '4',//井号
        name: 'GR1-4',//井名
        num: '371',//井节点数目
        cpr:'LRAT:999.9928 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '1266.302'},
                    {key:'YCOORD',unit:'m',value: '-702.4572'},
                    {key:'DEPTH',unit:'m',value: '1832.496'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '5',//井号
        name: 'GR2-2',//井名
        num: '355',//井节点数目
        cpr:'SHUT',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '2321.342'},
                    {key:'YCOORD',unit:'m',value: '-1500.041'},
                    {key:'DEPTH',unit:'m',value: '1884.405'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '6',//井号
        name: 'GR2-3',//井名
        num: '356',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '2435.878'},
                    {key:'YCOORD',unit:'m',value: '-1829.453'},
                    {key:'DEPTH',unit:'m',value: '1880.274'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '7',//井号
        name: 'GR2-4',//井名
        num: '331',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '2541.56'},
                    {key:'YCOORD',unit:'m',value: '-941.6433'},
                    {key:'DEPTH',unit:'m',value: '1887.254'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '8',//井号
        name: 'GR2-5',//井名
        num: '356',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
                // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '2726.393'},
                    {key:'YCOORD',unit:'m',value: '-1284.873'},
                    {key:'DEPTH',unit:'m',value: '1900.401'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]
            }
        ]
        
    },
    {
        id: '9',//井号
        name: 'GR3-2',//井名
        num: '389',//井节点数目
        cpr:'LRAT:999.9997 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '2769.661'},
                    {key:'YCOORD',unit:'m',value: '-1242.292'},
                    {key:'DEPTH',unit:'m',value: '1901.514'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '10',//井号
        name: 'GR3-3',//井名
        num: '363',//井节点数目
        cpr:'SHUT',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '3255.522'},
                    {key:'YCOORD',unit:'m',value: '-1449.746'},
                    {key:'DEPTH',unit:'m',value: '1889.97'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '11',//井号
        name: 'GR3-4',//井名
        num: '365',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '3354.514'},
                    {key:'YCOORD',unit:'m',value: '-551.0424'},
                    {key:'DEPTH',unit:'m',value: '1929.548'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '12',//井号
        name: 'GR3-6',//井名
        num: '363',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
                // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '3671.499'},
                    {key:'YCOORD',unit:'m',value: '-1282.311'},
                    {key:'DEPTH',unit:'m',value: '1925.202'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]
            }
        ]
        
    },
    {
        id: '13',//井号
        name: 'GR4-1',//井名
        num: '370',//井节点数目
        cpr:'LRAT:999.9999 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '3755.437'},
                    {key:'YCOORD',unit:'m',value: '-344.6722'},
                    {key:'DEPTH',unit:'m',value: '1960.381'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '14',//井号
        name: 'GR4-2',//井名
        num: '347',//井节点数目
        cpr:'SHUT',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '3925.16'},
                    {key:'YCOORD',unit:'m',value: '-686.4225'},
                    {key:'DEPTH',unit:'m',value: '1963.771'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '15',//井号
        name: 'GR4-3',//井名
        num: '319',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '4098.873'},
                    {key:'YCOORD',unit:'m',value: '-1049.915'},
                    {key:'DEPTH',unit:'m',value: '1950.633'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '16',//井号
        name: 'GR4-4',//井名
        num: '355',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
                // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '4092.854'},
                    {key:'YCOORD',unit:'m',value: '-166.9816'},
                    {key:'DEPTH',unit:'m',value: '1984.752'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]
            }
        ]
        
    },
    {
        id: '17',//井号
        name: 'GR4-5',//井名
        num: '351',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '4245.787'},
                    {key:'YCOORD',unit:'m',value: '-516.7445'},
                    {key:'DEPTH',unit:'m',value: '1989.236'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '18',//井号
        name: 'GR4-6',//井名
        num: '332',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '4504.464'},
                    {key:'YCOORD',unit:'m',value: '-870.1137'},
                    {key:'DEPTH',unit:'m',value: '1981.408'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '19',//井号
        name: 'GR5-1',//井名
        num: '382',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
                // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '3889.889'},
                    {key:'YCOORD',unit:'m',value: '-1840.704'},
                    {key:'DEPTH',unit:'m',value: '1912.83'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]
            }
        ]
        
    },
    {
        id: '20',//井号
        name: 'GR5-2',//井名
        num: '386',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '3932.508'},
                    {key:'YCOORD',unit:'m',value: '-2208.018'},
                    {key:'DEPTH',unit:'m',value: '1909.972'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '21',//井号
        name: 'GR5-5',//井名
        num: '318',//井节点数目
        cpr:'SHUT',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '4372.747'},
                    {key:'YCOORD',unit:'m',value: '-1994.812'},
                    {key:'DEPTH',unit:'m',value: '1920.678'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '22',//井号
        name: 'GR5-6',//井名
        num: '392',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '4591.463'},
                    {key:'YCOORD',unit:'m',value: '-2394.178'},
                    {key:'DEPTH',unit:'m',value: '1920.089'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '23',//井号
        name: 'GR6-2',//井名
        num: '357',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '4804.098'},
                    {key:'YCOORD',unit:'m',value: '-267.5754'},
                    {key:'DEPTH',unit:'m',value: '2016.597'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '24',//井号
        name: 'GR6-3',//井名
        num: '312',//井节点数目
        cpr:'SHUT',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '4987.155'},
                    {key:'YCOORD',unit:'m',value: '-626.5946'},
                    {key:'DEPTH',unit:'m',value: '2020.773'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '25',//井号
        name: 'GR6-4',//井名
        num: '316',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '5130.427'},
                    {key:'YCOORD',unit:'m',value: '-123.4141'},
                    {key:'DEPTH',unit:'m',value: '2015.812'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '26',//井号
        name: 'GR6-5',//井名
        num: '351',//井节点数目
        cpr:'LRAT:1000 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
              // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '5601.456'},
                    {key:'YCOORD',unit:'m',value: '-789.9283'},
                    {key:'DEPTH',unit:'m',value: '2018.281'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]  
            }
        ]
        
    },
    {
        id: '27',//井号
        name: 'GR7-2',//井名
        num: '273',//井节点数目
        cpr:'SHUT',//当前生产制度
        time: '0',//数据时间
        data:[
            {
               // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '5770.853'},
                    {key:'YCOORD',unit:'m',value: '-271.2598'},
                    {key:'DEPTH',unit:'m',value: '2032.128'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ] 
            }
        ]
        
    },
    {
        id: '28',//井号
        name: 'GR7-3',//井名
        num: '195',//井节点数目
        cpr:'LRAT:999.9997 SM3/DAY',//当前生产制度
        time: '0',//数据时间
        data:[
            {
                // 静态数据 rest
                rest: [
                    {key:'XCOORD',unit:'m',value: '5941.789'},
                    {key:'YCOORD',unit:'m',value: '285.394'},
                    {key:'DEPTH',unit:'m',value: '1992.729'},
                    {key:'STAGE',unit:'integer',value: '0'},
                    {key:'OUTLET',unit:'integer',value: '-1'},
                ],
                //动态数据 motion
                motion: [
                    {key:'WI',unit:'mD*m',value: '0'},//井筒内径
                    {key:'PRES',unit:'bar',value: '186.7146'},//压力
                    {key:'PCON',unit:'',value: '0'},//油
                    {key:'VM',unit:'m/day',value: '61038.07'},//井筒流速
                    {key:'QO',unit:'m3/day',value: '0'},//油oil
                    {key:'QG',unit:'m3/day',value: '0'},//气gas
                    {key:'QW',unit:'m3/day',value: '0'},//水water
                    {key:'WH',unit:'Frac',value: '0.5771189'},//持水率water
                    {key:'GH',unit:'Frac',value: '0.0064207'},//持气率gas
                    {key:'RS',unit:'Mscf/ST3',value: '1.36927'},//溶解气油比率
                ]
            }
        ]
    }
]

function isValidBufferGeometry(geometry) {
    if (!(geometry instanceof THREE.BufferGeometry)) {
      return false;
    }
  
    const position = geometry.attributes.position;
    if (!position) {
      return false;
    }
  
    // 检查顶点数组的长度是否合理
    if (position.count < 3) {
      return false;
    }
  
    // 检查顶点数组是否包含正确的数据类型
    if (position.array.constructor !== Float32Array) {
      return false;
    }
  
    // 可以继续检查其他属性，如法线、UV等
  
    return true;
}
  
function addColorBuffer(geometry){
      // 使用函数检查几何体
      const isValid = isValidBufferGeometry(geometry);
      if(!isValid){
          return
      }
      const pos = geometry.attributes.position;
      const count = pos.count; //顶点数量
  
          // 1. 计算模型y坐标高度差
      const xArr = [];//顶点所有x坐标，也就是地形高度
      const yArr = [];//顶点所有y坐标，也就是地形高度
      const zArr = [];//顶点所有z坐标，也就是地形高度
      for (let i = 0; i < count; i++) {
          xArr.push(pos.getX(i));//获取顶点x坐标，也就是地形高度
          yArr.push(pos.getY(i));//获取顶点y坐标，也就是地形高度
          zArr.push(pos.getZ(i));//获取顶点z坐标，也就是地形高度
      }
  
      xArr.sort();//数组元素排序，从小到大
      const minx = xArr[0];//x最小值
      const maxx = xArr[xArr.length - 1];//x最大值
      const xheight = maxx - minx; //山脉整体高度 
  
      yArr.sort();//数组元素排序，从小到大
      const miny = yArr[0];//y最小值
      const maxy = yArr[yArr.length - 1];//y最大值
      const yheight = maxy - miny; //山脉整体高度 
  
      zArr.sort();//数组元素排序，从小到大
      const minz = zArr[0];//z最小值
      const maxz = zArr[zArr.length - 1];//z最大值
      const zheight = maxz - minz; //山脉整体高度 
  
      //console.log("x:",minx,maxx,xheight);
      //console.log("y:",miny,maxy,yheight);
      //console.log("z:",minz,maxz,zheight);
      
      // 计算每个顶点的颜色值 // 2. 计算每个顶点的颜色值
      const colorsArr = [];
      
      // 根据顶点距离起点远近进行颜色插值计算
      let c1 = new THREE.Color(parseInt(colorsPalette[8].color)); //曲线起点颜色 蓝色 山谷颜色
      let c2 = new THREE.Color(parseInt(colorsPalette[0].color)); //曲线结束点颜色 红色 山顶颜色
      for (let i = 0; i < count; i++) {
          //const percent = i / count; //点索引值相对所有点数量的百分比
          // 红色分量从0到1变化，蓝色分量从1到0变化
          //colorsArr.push(percent, 0, 1 - percent); //蓝色到红色渐变色
          //根据顶点位置顺序大小设置颜色渐变
          //const c = c1.clone().lerp(c2, percent);//颜色插值计算
          //const c = new THREE.Color();
          //颜色插值结果，和c1一样rgb(1,0,0),50% c1 + 50% c2混合
          //c.lerpColors(c1,c2, 0.5);
          //colorsArr.push(c.r, c.g, c.b); 
  
          //梯度(gradient)、散度(divergence)与旋度(rotation)
          //梯度：标量求梯度得到矢量。
          //散度：矢量求散度得到标量。
          //旋度：矢量求旋度得到矢量。
  
          //当前高度和整体高度比值
          //const percent = (pos.getX(i) - minx) / xheight;
          //const c = c1.clone().lerp(c2, percent);//颜色插值计算
          //colorsArr.push(c.r, c.g, c.b); 
  
          //ymax: 2845 
          //ymin: 1750 
  
          if(pos.getY(i)>=1700 && pos.getY(i)<2000 ){
              c1 = new THREE.Color(parseInt(colorsPalette[8].color)); //曲线起点颜色 蓝色 山谷颜色
              c2 = new THREE.Color(parseInt(colorsPalette[8].color)); //曲线结束点颜色 红色 山顶颜色
          } else if(pos.getY(i)>=2000 && pos.getY(i)<2300 ){
              c1 = new THREE.Color(parseInt(colorsPalette[7].color)); //曲线起点颜色 蓝色 山谷颜色
              c2 = new THREE.Color(parseInt(colorsPalette[7].color)); //曲线结束点颜色 红色 山顶颜色
          } else if(pos.getY(i)>=2300 && pos.getY(i)<2400 ){
              c1 = new THREE.Color(parseInt(colorsPalette[6].color)); //曲线起点颜色 蓝色 山谷颜色
              c2 = new THREE.Color(parseInt(colorsPalette[6].color)); //曲线结束点颜色 红色 山顶颜色
          }  else if(pos.getY(i)>=2400 && pos.getY(i)<2500 ){
              c1 = new THREE.Color(parseInt(colorsPalette[5].color)); //曲线起点颜色 蓝色 山谷颜色
              c2 = new THREE.Color(parseInt(colorsPalette[5].color)); //曲线结束点颜色 红色 山顶颜色
          }  else if(pos.getY(i)>=2500 && pos.getY(i)<2600 ){
              c1 = new THREE.Color(parseInt(colorsPalette[4].color)); //曲线起点颜色 蓝色 山谷颜色
              c2 = new THREE.Color(parseInt(colorsPalette[4].color)); //曲线结束点颜色 红色 山顶颜色
          } else if(pos.getY(i)>=2600 && pos.getY(i)<2700 ){
              c1 = new THREE.Color(parseInt(colorsPalette[3].color)); //曲线起点颜色 蓝色 山谷颜色
              c2 = new THREE.Color(parseInt(colorsPalette[3].color)); //曲线结束点颜色 红色 山顶颜色
          } else if(pos.getY(i)>=2700 && pos.getY(i)<2800 ){
              c1 = new THREE.Color(parseInt(colorsPalette[2].color)); //曲线起点颜色 蓝色 山谷颜色
              c2 = new THREE.Color(parseInt(colorsPalette[1].color)); //曲线结束点颜色 红色 山顶颜色
          } else if(pos.getY(i)>=2800 && pos.getY(i)<2900 ){
              c1 = new THREE.Color(parseInt(colorsPalette[1].color)); //曲线起点颜色 蓝色 山谷颜色
              c2 = new THREE.Color(parseInt(colorsPalette[0].color)); //曲线结束点颜色 红色 山顶颜色
          }
  
          //console.log(pos.getY(i))
  
          const percent = (pos.getY(i) - miny) / yheight;
          const c = c1.clone().lerp(c2, percent);//颜色插值计算
          colorsArr.push(c.r, c.g, c.b); 
  
          //const percent = (pos.getZ(i) - minz) / zheight;
          //const c = c1.clone().lerp(c2, percent);//颜色插值计算
          //colorsArr.push(c.r, c.g, c.b); 
  
      }
      const colorBuffer = new Float32Array(colorsArr);
      return colorBuffer
}

function addBox(x=0,y=0,z=0){
    // 创建一个立方体
    const geometry = new THREE.BoxGeometry(80,80,80);
    const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
    const cube = new THREE.Mesh(geometry, material);

    let cubeGeometry = new THREE.BoxGeometry(80, 80, 80);
    let cubeEdges = new THREE.EdgesGeometry(cubeGeometry, 1);
    let edgesMtl =  new THREE.LineBasicMaterial({color: 0xdd2222});
    edgesMtl.depthTest = true;// 深度测试，若开启则是边框透明的效果
    let cubeLine = new THREE.LineSegments(cubeEdges, edgesMtl);
    //将创建的线框加入的几何体中
    cube.add(cubeLine);
    
    cube.position.set(x, y, z);
    scene.add(cube);
}

function addSphere(x=0,y=0,z=0){
    // 创建球体几何体
     //radius（必需）：球体的半径。
     //widthSegments（可选）：水平方向的分段数，决定了球体的水平切片数。
     //heightSegments（可选）：垂直方向的分段数，决定了球体的纵向切片数。
     //phiStart（可选）：纵向起始角度，以弧度表示。
     //phiLength（可选）：纵向角度范围，以弧度表示。
     //thetaStart（可选）：水平起始角度，以弧度表示。
     //thetaLength（可选）：水平角度范围，以弧度表示。
     const radius = 50;
     const widthSegments = 32;
     const heightSegments = 16;
     const geometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);

     // 创建材质
     const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });

     // 创建球体网格
     const sphere = new THREE.Mesh(geometry, material);
     sphere.position.set(x,y,z)
     scene.add(sphere); 
}

 function addsprite (color, text, postion) {
     let sprite = null
     let spriteMaterial = addspriteMaterial(color, text)
     sprite = new THREE.Sprite(spriteMaterial)
     sprite.scale.set(450, 150, 50) // 大小缩放比例
     // 在使用透视投影相机对象的时候，精灵模型对象显示的大小和网格模型一样受距离相机的距离影响，也就是距离越远，显示效果越小
     // 创建精灵模型对象，不需要几何体geometry参数
     // 更新材质
     sprite.material = spriteMaterial
     // 控制精灵大小，比如可视化中精灵大小表征数据大小 位置 只需要设置x、y两个分量就可以
     // 更新车辆标签位置
     sprite.position.set(postion.x, postion.y, postion.z)
     // 将geometry的groupsNeedUpdate属性设置为true来告知渲染器该对象需要更新
     sprite.geometry.groupsNeedUpdate = true
     return sprite
 }

 function addspriteMaterial (color, text) { 
     let texture = new THREE.Texture(createCanvas(color, text))
     texture.needsUpdate = true
     // 创建精灵材质对象SpriteMaterial
     let spriteMaterial = new THREE.SpriteMaterial({
       transparent: true,
       map: texture // 设置精灵纹理贴图
     })
     return spriteMaterial
 }

 function createCanvas (color, text) {
     // 精灵平面（Sprite）是一个在3D场景中总是面对着相机的平面
     // 利用canvas绘制文字，作为sprite的材质 canvas 画布
     let canvas = document.createElement('canvas')
     // canvas 绘图上下文环境
     let context = canvas.getContext('2d')
     canvas.width = 120
     canvas.height = 40
     context.scale(2, 2)
     drawRect(context, color) // 绘制圆角矩形
     /* 字体颜色 */
     context.fillStyle = '#fff'
     // 减少阴影效果
     context.shadowColor = '#fff'
     context.shadowBlur = 1
     context.font = 'lighter 14px serif'
     /* 文字 */
     context.fillText(text, 1.9, 14.5)
     return canvas
 }

 function drawRect (ctx, color) {
     let width = 60
     let height = 20
     // 绘制一个圆角矩形
     strokeRoundRect(ctx, 1, 1, width - 2, height - 2, [2, 2, 2, 2], 1, color)
 }

 function strokeRoundRect (cxt, x, y, width, height, radius, lineWidth, strokeColor) {
     // 圆的直径必然要小于矩形的宽高
     if (2 * radius > width || 2 * radius > height) { return false }
     cxt.save()
     cxt.translate(x, y)
     // 绘制圆角矩形的各个边
     drawRoundRectPath(cxt, width, height, radius)
     cxt.lineWidth = lineWidth || 2 // 若是给定了值就用给定的值否则给予默认值2
     cxt.strokeStyle = strokeColor
     cxt.fillStyle = strokeColor + '88'
     cxt.fillRect(0, 0, width, height)
     cxt.stroke()
     cxt.restore()
 }

 function drawRoundRectPath (cxt, width, height, radius) {
     let r0 = radius[0]
     let r1 = radius[1]
     let r2 = radius[2]
     let r3 = radius[3]
     cxt.beginPath(0)
     // 从右下角顺时针绘制，弧度从0到1/2PI
     cxt.arc(width - r0, height - r0, r0, 0, Math.PI / 2)
     // 矩形下边线
     cxt.lineTo(r1, height)
     // 左下角圆弧，弧度从1/2PI到PI
     cxt.arc(r1, height - r1, r1, Math.PI / 2, Math.PI)
     // 矩形左边线
     cxt.lineTo(0, r2)
     // 左上角圆弧，弧度从PI到3/2PI
     cxt.arc(r2, r2, r2, Math.PI, Math.PI * 3 / 2)
     // 上边线
     cxt.lineTo(width - r3, 0)
     // 右上角圆弧
     cxt.arc(width - r3, r3, r3, Math.PI * 3 / 2, Math.PI * 2)
     // 右边线
     cxt.lineTo(width, height - r3)
     cxt.closePath()
}

function createBatchMesh(){
    wellInfo.map((item)=>{
        let { id, name, num, data, cpr } = item;
        let XCOORD = '0', YCOORD  = '0', DEPTH  = '0', STAGE  = '0';
        data[0].rest.map((it)=>{
            if(it.key==='XCOORD'){
                XCOORD = it.value
            }
            if(it.key==='YCOORD'){
                YCOORD = it.value
            }
            if(it.key==='DEPTH'){
                DEPTH = it.value
            }
            if(it.key==='STAGE'){
                STAGE = it.value
            }
        });
        
        // 位置矫正 补偿 XCOORD, YCOORD, DEPTH
        let x = ((XCOORD0/2000 + parseFloat(XCOORD))/1).toFixed(3);
        let y = 500; // = (parseInt(parseFloat)/50).toFixed(0);
        let z = ((YCOORD0/3000 + parseFloat(YCOORD))/1).toFixed(3);
        //console.log(x,y,z);
        x = x-4000;
        z = z+3000;
        let wellCyl = createMesh(x, y, z);
        wellCyl.name = "well_" + name;
        wellItems.add(wellCyl);

        let sprite = addsprite(cpr==='SHUT'?'#333333':'#00ff00', 
            name + '-' + cpr.substr(0, 2),
            {x: x, y: 1300, z: z});
        sprite.name = "sprite_" + name;
        wellSprites.add(sprite);
        
    })

    // 油井 标牌 网格对象 批量添加到场景中
    scene.add(wellItems)
    scene.add(wellSprites);
}

// 创建网格模型
function createMesh(x=0,y=0,z=0) {
    //创建圆柱
    //属性	必须	描述
    //radiusTop	否	该属性定义圆柱顶部圆的半径，默认值是20
    //radiusBottom	否	该属性定义圆柱底部圆的半径，默认值是20
    //height	否	该属性定义圆柱的高度，默认是100
    //segmentsX	否	该属性指定沿X轴分成多少段，段数越多，圆柱越光滑，默认值是8
    //segmentsY	否	该属性指定沿Y轴分成多少段，段数越多，圆柱竖直方向分的面越多，默认值是1
    //openEnded	否	该属性用来指定圆柱顶部和底部是否封闭，默认false

    const geom = new THREE.CylinderGeometry(
      properties.radiusTop.value,
      properties.radiusBottom.value,
      properties.height.value,
      properties.segmentsX.value,
      properties.segmentsY.value,
      properties.openEnded
    )
    // 创建材质
    const meshMaterial = new THREE.MeshNormalMaterial({
      side: THREE.DoubleSide
    })
    const wireFrameMat = new THREE.MeshBasicMaterial({ wireframe: true })

    // 添加组合材质
    let meshCyl = createMultiMaterialObject(geom, [
      meshMaterial,
      wireFrameMat
    ])

    meshCyl.position.set(x, y, z);

    return meshCyl;
}


/**
 * 材质更新调整
 */
function updateAllMaterials() {

    //scene.getObjectByName('oil-mesh')
    model.traverse((child) => {
        if (child instanceof THREE.Mesh) {
            child.castShadow = true
            child.receiveShadow = true
            // child.material.envMap = TEXTURE.envMap
            child.material.envMap = scene.userData.dynamicMap
            // ssrPass.selects.push(child)

            // console.log(child.material.name)

            if (child.material.name === 'rubber') {
                child.material.color.set('#222')
                child.material.roughness = 1
                child.material.normalScale.set(4, 4)
                child.material.envMap = scene.userData.dynamicMap
            }
            if (child.material.name === 'window') {
                child.material.color.set('#222')
                child.material.roughness = 0
                child.material.clearcoat = 0.1
                child.material.envMap = scene.userData.dynamicMap
            }
            if (child.material.name === 'coat') {
                child.material.envMapIntensity = 4
                child.material.roughness = 0.5
                child.material.metalness = 1
                child.material.envMap = scene.userData.dynamicMap
            }
            if (child.material.name === 'paint') {
                child.material.envMapIntensity = 2
                child.material.roughness = 0.45
                child.material.metalness = 0.8
                child.material.envMap = scene.userData.dynamicMap
                child.material.color.set(debugObject.paintColor)
            }
        }
    })

    // 更新阴影
    scene.getObjectByProperty('type', 'SpotLight').shadow.needsUpdate = true
}

export { createModels, updateAllMaterials }
